<?php

namespace App\Http\Controllers;

use App\Models\BillType;
use Illuminate\Http\Request;

class BillTypeController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
        $billTypes = BillType::query()
            ->where('is_active', 1)      // 假设有状态字段
            ->orderBy('id')      // 假设有排序字段
            ->get(['id', 'flow', 'name','icon']); // 指定返回字段
        return response()->json([
            'code' => 200,
            'data' => $billTypes,
            'message' => '请求成功'
        ], 200
        );
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        // 验证请求数据
        $validated = $request->validate([
            'code' => 'required|string|max:30|unique:bill_types,code',
            'name' => 'required|string|max:50',
            'flow' => 'required|in:income,outcome',
            'icon' => 'required|string',
            'is_active' => 'sometimes|boolean',
            'sort_order' => 'sometimes|integer'
        ]);

        // 创建账单类型
        $billType = BillType::create($validated);

        // 返回响应
        return response()->json([
            'code' => 201,
            'data' => $billType,
            'message' => '账单类型添加成功'
        ], 201);
    }

    /**
     * Display the specified resource.
     */
    public function show(BillType $billType)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(BillType $billType)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, BillType $billType)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(BillType $billType)
    {
        //
    }
}
